/**
 * 
 */
package com.tvshows.front.view.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

import com.tvshows.front.listener.dialog.DialogListener;
import com.tvshows.front.ui.dialog.DialogAddFavoriteUI;
import com.tvshows.front.view.labels.FrontButton;
import com.tvshows.front.view.labels.FrontLabel;
import com.tvshows.front.view.labels.FrontText;
import com.tvshows.interactions.ObserverBdd;
import com.tvshows.structure.bdd.BddQueries;
import com.tvshows.structure.beans.Favorite;

/**
 * @author Tangi Midy
 */
public class DialogAddFavoriteView
    extends Composite
    implements DialogAddFavoriteUI
{

    private Group informationGroup;

    private Group validationGroup;

    private FrontText nameText, seasonText, lastEpisodeText;

    private FrontLabel nameLabel, seasonLabel, lastEpisodeLabel;

    private FrontButton okButton;

    private FrontButton cancelButton;

    private DialogListener listener;

    private boolean modify = false;

    /**
     * @param parent
     * @param style
     */
    public DialogAddFavoriteView( Composite parent, int style )
    {
        super( parent, style );
        this.getShell().setText( "Ajouter un favori" );
        initGroups();
        initButtons();
        initListener();
    }

    /**
     * @param favorite
     */
    private void initFields( Favorite favorite )
    {
        nameText.setText( favorite.getNameFavorite() );
        seasonText.setText( favorite.getSeasonNumber() );
        lastEpisodeText.setText( favorite.getLastEpisodeNumber() );
    }

    private void initListener()
    {
        listener = new DialogListener( this );
        okButton.addListener( SWT.Selection, listener );
        cancelButton.addListener( SWT.Selection, listener );
    }

    private void initGroups()
    {
        informationGroup = new Group( this, SWT.BORDER );
        informationGroup.setText( "Informations sur la série" );
        informationGroup.setBounds( 10, 10, 300, 150 );

        validationGroup = new Group( this, SWT.NONE );
        validationGroup.setLayout( new RowLayout( SWT.HORIZONTAL ) );
        validationGroup.setBounds( 10, 10 + 150, 300, 60 );

    }

    private void initButtons()
    {

        nameLabel = new FrontLabel( informationGroup, SWT.NONE );
        nameLabel.setText( "Nom du favori : " );
        nameLabel.setBounds( 5, 30, 100, 25 );
        nameText = new FrontText( informationGroup, SWT.NONE );
        nameText.setBounds( 115, 30, 150, 20 );

        seasonLabel = new FrontLabel( informationGroup, SWT.NONE );
        seasonLabel.setText( "Saison en cours : " );
        seasonLabel.setBounds( 5, 60, 100, 25 );
        seasonText = new FrontText( informationGroup, SWT.NONE );
        seasonText.setBounds( 115, 60, 20, 20 );

        lastEpisodeLabel = new FrontLabel( informationGroup, SWT.NONE );
        lastEpisodeLabel.setText( "Dernier épisode : " );
        lastEpisodeLabel.setBounds( 5, 90, 100, 25 );
        lastEpisodeText = new FrontText( informationGroup, SWT.NONE );
        lastEpisodeText.setBounds( 115, 90, 20, 20 );

        okButton = new FrontButton( validationGroup, SWT.NONE );
        okButton.setText( "Valider" );
        okButton.setBounds( 45, 20, 100, 25 );
        cancelButton = new FrontButton( validationGroup, SWT.NONE );
        cancelButton.setText( "Annuler" );
        cancelButton.setBounds( 155, 20, 100, 25 );

    }

    /**
     * @return the okButton
     */
    public Button getOkButton()
    {
        return okButton;
    }

    /**
     * @return the cancelButton
     */
    public Button getCancelButton()
    {
        return cancelButton;
    }

    public String[] getTexts()
    {
        return new String[] { nameText.getText(), seasonText.getText(), lastEpisodeText.getText() };
    }

    /**
     * @param modify modification or not
     */
    public void setModify( boolean modify )
    {
        this.modify = modify;
        if ( ObserverBdd.getObserverBdd().getListFavorite().isToModify() && modify)
        {
            Favorite favorite =
                BddQueries.getBddQueries().loadFavorite(
                    ObserverBdd.getObserverBdd().getListFavorite().getSelectedFavorite() );
            initFields( favorite );
        }
    }

    /**
     * @return the modify
     */
    @Override
    public boolean isModify()
    {
        return modify;
    }

    @Override
    public void initFormData()
    {
        // TODO Auto-generated method stub

    }

    @Override
    public void initComposites()
    {
        // TODO Auto-generated method stub

    }
}
